var orderCount = 0

function addOrder() {
	orderCount++
	var orderDiv = document.createElement('div')
	orderDiv.className = 'order'
	var label = document.createElement('label')
	label.textContent = '第' + orderCount + '单'
	var input = document.createElement('input')
	input.type = 'number'

	input.placeholder = '订单利润'
	orderDiv.appendChild(label)
	orderDiv.appendChild(input)
	document.getElementById('orders').appendChild(orderDiv)
}

function calculateCommission() {
	var orders = document.getElementsByClassName('order')
	var commission = 0
	for (var i = 0; i < orders.length; i++) {
		var profit = orders[i].children[1].value
		var rate = i < 2 ? 0.3 : i < 4 ? 0.4 : 0.5
		commission += ((profit * 2) / 3) * rate
	}
	document.getElementById('commission').value = commission.toFixed(2)
}

function generateRows(orderNeedCount) {
	let arr = []
	let commissionStr = ''
	for (let i = 1; i <= orderNeedCount; i++) {
		let commissionRate
		if (i <= 2) {
			// 第1单和第2单的提成比例是利润的2/3×30%
			commissionRate = '2/3×30%'
		} else if (i <= 4) {
			// 第3单和第4单的提成比例是利润的2/3×40%
			commissionRate = '2/3×40%'
		} else {
			// 第5单及以上的提成比例是利润的2/3×50%
			commissionRate = '2/3×50%'
		}
		commissionStr += (i > 1 ? ' + ' : '') + '第' + i + '单利润×' + commissionRate
		arr.push([i.toString(), commissionStr])
	}
	return arr
}

// 创建一个新的table元素
let table = document.getElementById('commissionTable')

// 创建thead元素和tr元素
let thead = document.createElement('thead')
let tr = document.createElement('tr')

// 创建两个th元素，并设置它们的文本内容
let th1 = document.createElement('th')
th1.textContent = '单数'
let th2 = document.createElement('th')
th2.textContent = '提成计算'

// 将th元素添加到tr元素中
tr.appendChild(th1)
tr.appendChild(th2)

// 将tr元素添加到thead元素中
thead.appendChild(tr)

// 将thead元素添加到table元素中
table.appendChild(thead)

// 创建tbody元素
let tbody = document.createElement('tbody')

// 创建一个数组，包含所有的行数据
// 测试函数
let orderNeedCount = 5 // 假设订单数量是5
let theRows = generateRows(orderNeedCount)

// 遍历行数据，为每一行创建一个新的tr元素和td元素
for (let i = 0; i < theRows.length; i++) {
	let tr = document.createElement('tr')

	for (let j = 0; j < theRows[i].length; j++) {
		let td = document.createElement('td')
		td.textContent = theRows[i][j]
		tr.appendChild(td)
	}

	tbody.appendChild(tr)
}

// 将tbody元素添加到table元素中
table.appendChild(tbody)
let modalContent = document.getElementsByClassName('modal-content')
// 将table元素添加到DOM中
modalContent[0].appendChild(table)

var showModal = document.getElementsByClassName('rule')[0]
showModal.onclick = function () {
	var modal = document.getElementById('myModal')
	modal.style.display = 'block'
}
var close = document.getElementsByClassName('close')[0]
close.onclick = function () {
	var modal = document.getElementById('myModal')
	modal.style.display = 'none'
}
